import { queryAll, remove, query ,create,update, deleteBatch,invalid } from '../services/customerInformations';
import { message } from 'antd';

export default {

  namespace: 'customerInformation',

  state: {
    load : true,
    list: [],
    curItem: {},
    modalVisible: false,
    modalType: 'create',
    record:[],
    selectedRowKeys:[],
    index:-1,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const pathname = location.pathname
        if(pathname === '/customerInformation'){
          dispatch({
            type: 'query',
            payload: location.query,
          })
        }
      })
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {  // eslint-disable-line
      yield put({ type: 'save' });
    },
    *query({ payload }, { call, put }){
      const data = yield call(queryAll)
      //console.log('Model:',data);
      yield put ({type: 'hideLoading'})

      if(data){
        yield put({
          type: 'querySuccess',
          payload: {
            list: data,
          },
        })
      }
    },


    *'delete' ({ payload }, { call, put }) {
        console.log('4444',payload);
      const data=yield call(remove, {payload})
        if(data){
        yield put({ type: 'query'})
        message.success('删除成功！',1)
      }
    },

    *deleteBatch({ payload }, { call, put }) { //批量删除
      //console.log('deleteBatch>>>',payload);
         yield call(deleteBatch, payload )
         yield put({type: 'query'})
         message.success('删除成功！',1)
       },
    *create ({ payload }, { call, put }) {
       //console.log('payload',payload);
      const item = payload;
      yield call(create, payload)
      yield put({type: 'query'})
      message.warning('增加成功！',1)

    },

    *onInvalid({ payload }, { call, put }) { //撤户
         yield call(invalid, payload )
         yield put({type: 'query'})

         message.success('完成！',1)

       },
    *update ({ payload }, { select, call, put }) {

      const id = yield select(({ customerInformation }) => customerInformation.curItem.id)
      const newCustomer = { ...payload, id }
      const data = yield call(update, newCustomer)
      if (data) {
      yield put({type: 'hideModal'})
      yield put({ type: 'query'})
      message.success('修改成功！',1)
    } else {
      throw data
      }
    },

    *search({ payload }, {call, put ,select}){
      const {customerName, customerNum, customerEname, customerAbbreviation,
              customerProperty, customerType, industryCategory, isServing} = payload;
      const list = yield call(queryAll);
      //console.log('model>>>>>',customerProperty);
      let data = list;
        if (customerName) {
          data = data.filter((item)=>item.customerName && item.customerName.match(customerName));
        }
        if (customerNum) {
          data = data.filter((item)=>item.customerNum && item.customerNum.match(customerNum));
        }
        if (customerEname) {
          data = data.filter((item)=>item.customerEname && item.customerEname.match(customerEname));
        }
        if (customerAbbreviation) {
          data = data.filter((item)=>item.customerAbbreviation && item.customerAbbreviation.match(customerAbbreviation));
        }
        if (customerProperty && customerProperty.length > 0 ) {
          data = data.filter((item)=>item.customerProperty==customerProperty);
        }

        if (customerType) {
          data = data.filter((item)=>item.customerType==customerType);
        }

        if (industryCategory) {
          data = data.filter((item)=>item.industryCategory==industryCategory);
        }
        // if (industryCategory) {
        //   data = data.filter((item)=>item.industryCategory && item.industryCategory.match(industryCategory));
        // }

        if (isServing || isServing ===0) {

          data = data.filter((item)=>item.isServing==isServing);
        }

      if(data){
        yield put({
          type: 'querySuccess',
          payload: {
            list: data,
          },
        })
      }
    }
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    querySuccess (state, action) {
      const { list } = action.payload
      return { ...state,
        list,
       }
    },

    showModal (state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal (state) {
      return { ...state, modalVisible: false }
    },
    //隐藏加载
    hideLoading (state,action) {
      return {
        load: false
      }
    },
  },
};
